package com.wanshi.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanshi.member.base.Page;
import com.wanshi.member.base.Result;
import com.wanshi.member.entity.Member;
import com.wanshi.member.mapper.MemberMapper;
import com.wanshi.member.req.MemberREQ;
import com.wanshi.member.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员信息表 服务实现类
 * </p>
 *带分页的条件查询
 *
 * 删除
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-05-08
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Override
    public Result search(long page, long size, MemberREQ req) {
        QueryWrapper query = new QueryWrapper();
        if(req != null) {
            if(StringUtils.isNotBlank(req.getName())) {
                query.like("name", req.getName());
            }
            if(StringUtils.isNotBlank(req.getCardNum())) {
                query.like("card_num", req.getCardNum());
            }
            if(req.getBirthday() != null) {
                query.eq("birthday", req.getBirthday());
            }
            if(StringUtils.isNotBlank(req.getPayType())) {
                query.eq("pay_type", req.getPayType());
            }
        }

        query.orderByDesc("id");

        // 封装分页对象
        IPage<Member> p = new Page<>(page ,size);
        IPage<Member> data = baseMapper.selectPage(p, query);
        return Result.ok(data);
    }

    @Override
    public Result update(int id, Member member) {
        if(member.getId() == null) {
            member.setId(id);
        }

        int i = baseMapper.updateById(member);
        if(i < 1){
            return Result.error("修改会员信息失败");
        }

        return Result.ok();
    }
}
